package com.taxshare.purchaser.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taxshare.mybatis.utils.DozerBeanUtil;
import com.taxshare.purchaser.constant.CodeConstant;
import com.taxshare.purchaser.constant.ResultConstant;
import com.taxshare.purchaser.entity.InvoiceBlackRulesEntity;
import com.taxshare.purchaser.entity.InvoiceBuyerRulesEntity;
import com.taxshare.purchaser.entity.InvoiceReviewRulesEntity;
import com.taxshare.purchaser.mapper.InvoiceBuyerRulesMapper;
import com.taxshare.purchaser.view.dto.InvoiceBuyerRulesDTO;
import com.taxshare.response.beans.Response;
import com.taxshare.tools.util.UUIDUitl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
/**
 * @ClassName: InvoiceBuyerRulesService
 * @Description: 购方审核规则操作
 * @author: hjh
 * @date: 2021/4/28 14:46
 */
@Slf4j
@Service
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.READ_COMMITTED)
public class InvoiceBuyerRulesService extends ServiceImpl<InvoiceBuyerRulesMapper,InvoiceBuyerRulesEntity> {

    public Response updateBuyerRules(InvoiceBuyerRulesDTO bean) {
        if(StrUtil.hasBlank(bean.getUuid())){
            return Response.error(ResultConstant.RET_R0001_CODE, ResultConstant.RET_R0001_MSG);
        }
        if(StrUtil.hasBlank(bean.getBuyerNotPassReasons())){
            return Response.error(ResultConstant.RET_R0002_CODE, ResultConstant.RET_R0002_MSG);
        }
        if (StrUtil.hasBlank(bean.getBuyerTicketType())||(!CodeConstant.INVOICE_TYPES_LIST.contains(bean.getBuyerTicketType()))) {
            return Response.error(ResultConstant.RET_R0001_CODE, ResultConstant.RET_R0001_MSG);
        }
        if(StrUtil.hasBlank(bean.getBuyerSensitiveHead())){
            return Response.error(ResultConstant.RET_R0003_CODE, ResultConstant.RET_R0003_MSG);
        }
        //首次没有buyerRuleId，添加
        InvoiceBuyerRulesEntity flg = baseMapper.selectOne(new QueryWrapper<InvoiceBuyerRulesEntity>().eq("uuid",bean.getUuid()));
        if(flg==null){
            InvoiceBuyerRulesEntity entity = DozerBeanUtil.map(bean, InvoiceBuyerRulesEntity.class);
            entity.setBuyerRuleId(UUIDUitl.UUID32(false));
            entity.setUuid(bean.getUuid());
            int result = baseMapper.insert(entity);
            if(result!=1){
                return Response.error(ResultConstant.RET_D0001_CODE,ResultConstant.RET_D0001_MSG);
            }
            return Response.success();
        }
        //如果有buyerRuleId，内容更新
        UpdateWrapper<InvoiceBuyerRulesEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("uuid",bean.getUuid())
                     .set("buyer_not_pass_reasons",bean.getBuyerNotPassReasons())
                     .set("buyer_ticket_type",bean.getBuyerTicketType())
                     .set("buyer_sensitive_head",bean.getBuyerSensitiveHead());
        InvoiceBuyerRulesEntity entity = new InvoiceBuyerRulesEntity();
        int result = baseMapper.update(entity,updateWrapper);
        if(result!=1){
            return Response.error(ResultConstant.RET_D0002_CODE,ResultConstant.RET_D0002_MSG);
        }
        return Response.success();
    }
    /**
     * 更新是否开启状态
     * @param bean
     * @return
     */
    public Response updateBuyerIsOpen(InvoiceBuyerRulesDTO bean) {
        if(StrUtil.hasBlank(bean.getUuid())){
            return Response.error(ResultConstant.RET_R0001_CODE, ResultConstant.RET_R0001_MSG);
        }
        if(!CodeConstant.RULES_OPEN.equals(bean.getBuyerIsOpen())&&(!CodeConstant.RULES_NOT_OPEN.equals(bean.getBuyerIsOpen()))){
            return Response.error(ResultConstant.RET_R0001_CODE, ResultConstant.RET_R0001_MSG);
        }
        InvoiceBuyerRulesEntity flg = baseMapper.selectOne(new QueryWrapper<InvoiceBuyerRulesEntity>().eq("uuid",bean.getUuid()));
        //首次添加
        if(flg==null){
            InvoiceBuyerRulesEntity entity = new InvoiceBuyerRulesEntity();
            entity.setBuyerRuleId(UUIDUitl.UUID32(false));
            entity.setUuid(bean.getUuid());
            entity.setBuyerIsOpen(bean.getBuyerIsOpen());
            int result = baseMapper.insert(entity);
            if(result!=1){
                return Response.error(ResultConstant.RET_D0001_CODE,ResultConstant.RET_D0001_MSG);
            }
            return Response.success();
        }
        //非首次更新
        UpdateWrapper<InvoiceBuyerRulesEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("uuid",bean.getUuid())
                     .set("buyer_is_open",bean.getBuyerIsOpen());
        InvoiceBuyerRulesEntity entity = new InvoiceBuyerRulesEntity();
        int result = baseMapper.update(entity,updateWrapper);
        if(result!=1){
            return Response.error(ResultConstant.RET_D0002_CODE,ResultConstant.RET_D0002_MSG);
        }
        return Response.success();
    }
}
